﻿using Common;
using DataLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Core.Objects;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImageUploaderServer
{
    public partial class Form_ServerList : Form
    {
        public Form_ServerList()
        {
            InitializeComponent();
        }
        private ImageManagerEntities ime = new ImageManagerEntities();
        private PublicMethod pm = new PublicMethod();
        private void Form_ServerList_Load(object sender, EventArgs e)
        {
            txt_LocalServer.Text = pm.GetLocalIP();
            var serverList = ime.ServerInfo.Select(u=>u.ServerIP).ToList();
            foreach (var server in serverList)
            {
                listBox1.Items.Add(server);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string server = listBox1.SelectedItem.ToString();
            GetDiskInfo(server);
        }

        private void GetDiskInfo(string serverip)
        {
            var server = ime.ServerInfo.Where(u => u.ServerIP == serverip).First();
            var disks = server.ServerDisk.Where(u => u.ServerID == server.ServerID);
            if (disks != null && disks.Count() > 0)
            {
                foreach (var disk in disks)
                {
                    ShowInfo(ime.DiskInfo.Find(disk.DiskID));
                }
            }
            else
            {
                listBox2.Items.Add(string.Format("服务器已移除"));
                ime.ServerInfo.Remove(server);
                ime.SaveChanges();
            }
            
        }
        private void ShowInfo(DiskInfo diskinfo)
        {
            long gb = 1024 * 1024 * 1024;
            string type = "";
            string str = "";
            double freePath = 0d;
            str = " 磁盘名称:" + diskinfo.DiskName;
            freePath = Math.Round(Convert.ToDouble(diskinfo.DiskFree) / gb, 1);
            str += " 可用空间:" + freePath + "G";
            str += " 实际大小:" + Math.Round(Convert.ToDouble(diskinfo.DiskSize) / gb, 1) + "G";
            if (freePath < 200)
            {
                str += " ----空间不足";
                Email.SendEmail("wtlemon@126.com", "服务器空间不足", "服务器" + txt_LocalServer + diskinfo.DiskName + "卷标(" + diskinfo.DiskLabel + ")" + "空间不足");
            }
            listBox2.Items.Add(str);
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(txt_TargetIP.Text) || string.IsNullOrEmpty(txt_TargetPort.Text))
            {
                MessageBox.Show("请完善服务器信息");
                return;
            }
            ServerInfo serverInfo = new ServerInfo();
            serverInfo.ServerIP = txt_TargetIP.Text;
            serverInfo.ServerPort = txt_TargetPort.Text;
            serverInfo.Created_at = DateTime.Now;
            serverInfo.Updated_at = DateTime.Now;
            ime.ServerInfo.Add(serverInfo);
            ime.SaveChanges();
            listBox2.Items.Add("添加了1台服务器,IP:" + txt_TargetIP.Text + ",Port:" + txt_TargetPort.Text);
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            string serverip = listBox2.SelectedItem.ToString();
            var server = ime.ServerInfo.Where(u => u.ServerIP == serverip).First();
            listBox2.Items.Add(string.Format("服务器已移除"));
            ime.ServerInfo.Remove(server);
            ime.SaveChanges();
        }
    }
}
